package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.request.NoticePageRequest;
import com.example.springboot.entity.Notice;
import com.example.springboot.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin  //解决跨域错误
@RestController
@RequestMapping("/notice")
//是rest风格的noticecontroller
public class NoticeController {

    @Autowired
    INoticeService noticeService;

    //    NoticePageRequest传入用户名和电话信息
    @GetMapping("/page")
    public Result page(NoticePageRequest noticePageRequest){
        //        返回数据 Result.success传入一个对象
        return Result.success(noticeService.page(noticePageRequest));

    }

//    post put get delete对应增改查删四类操作
    //    新增会员
    @PostMapping("/save")
    public Result save(@RequestBody Notice notice){
        noticeService.save(notice);
        return Result.success();
    }

//    更新会员信息
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice){
        noticeService.update(notice);
        return Result.success();
    }

    //    获取用户列表
    @GetMapping("/list")
    public Result list(){
        //        返回数据
        List<Notice> notices=noticeService.list();
//        System.out.println("notices内容是"+notices);
        return Result.success(notices);

    }


//    根据ID更新用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Notice notice =noticeService.getById(id);
        return Result.success(notice);

    }

    //删除操作:
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        noticeService.deleteById(id);
        return Result.success();
    }



    /*
    最初是通过List返回所有数据,而建立Result后采用Result返回数据
    @GetMapping("/list")
    public List<Notice> listNotices(){
        return noticeService.listNotices();
    }
     */
}
